package com.uniledger.exchange;

import android.app.Activity;
import android.arch.persistence.room.Room;
import android.support.multidex.MultiDexApplication;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.uniledger.commonlib.util.ToastUtils;
import com.uniledger.commonlib.util.Utils;
import com.uniledger.exchange.db.UniDatabase;
import com.uniledger.exchange.home.scheme.PayOkActivity;
import com.uniledger.exchange.home.scheme.PayQRCodeActivity;
import com.uniledger.exchange.push.NotifyUtil;
import com.uniledger.exchange.welcome.WelcomeActivity;
import com.uniledger.languagelib.MultiLanguageUtil;

import net.danlew.android.joda.JodaTimeAndroid;

import butterknife.ButterKnife;
import io.reactivex.functions.Consumer;
import io.reactivex.plugins.RxJavaPlugins;

public class UniApp extends MultiDexApplication implements AppLifeCycleCallback.BecameListener {

    private static UniApp instance; //APP单例
    private static UniDatabase db; //数据库单例
    private static AppLifeCycleCallback lifeCycleCallback;//Activity的生命周期监测

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    /**
     * 获取APP单例
     *
     * @return
     */
    public static UniApp getInstance() {
        return instance;
    }

    /**
     * 获取数据库实例
     *
     * @return
     */
    public static UniDatabase getDb() {
        return db;
    }

    /**
     * 初始化操作
     */
    private void init() {

        //多语言设置
        MultiLanguageUtil.init(this);


        //应用前后台监听：注册监听每个Activity的生命周期，应用前后台切换 需要可以监听当前方法
        lifeCycleCallback = new AppLifeCycleCallback(this, this);
        registerActivityLifecycleCallbacks(lifeCycleCallback);

        //时间工具类初始化
        JodaTimeAndroid.init(this);

        //工具类初始化
        Utils.init(this);

        //土司工具类初始化
        ToastUtils.init(this);

        //视图绑定机制
        ButterKnife.setDebug(BuildConfig.DEBUG);

        //初始化日志
        initLog();

        //初始化数据库
        initDb();

        //Bugly初始化

        //推送通知格式化
        NotifyUtil.init(this);

        initBugly();

        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

            }
        });
    }

    private void initBugly(){
        Beta.canShowUpgradeActs.add(HomeActivity.class);
        Bugly.init(getApplicationContext(), "70431c687a", BuildConfig.DEBUG);
    }

    /**
     * 初始化数据库
     */
    private void initDb() {
        db = Room.databaseBuilder(this,
                UniDatabase.class, "uni1-ledger-db").build();
    }

    /**
     * 应用日志初始化
     */
    private void initLog() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag("Tiger")
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                //TODO 日志开关
                return BuildConfig.DEBUG;
            }
        });
    }

    /**
     * 前台运行
     *
     * @param activity
     */
    @Override
    public void onBecameForeground(Activity activity) {
        //TODO 锁屏页面除外
        if (activity instanceof WelcomeActivity) {//启动页操作

        } else if (activity instanceof PayQRCodeActivity) {

        } else if (activity instanceof PayOkActivity) {

        } else {
            //TODO 前后台切换，不知道所处页面
        }
    }

    /**
     * 前台->后台
     *
     * @param activity
     */
    @Override
    public void onBecameBackground(Activity activity) {

    }

    /**
     * 正常使用
     *
     * @param activity
     */
    @Override
    public void onNormal(Activity activity) {
        if (activity instanceof PayQRCodeActivity) {

        } else if (activity instanceof PayOkActivity) {

        } else {
            //TODO 前后台切换，不知道所处页面
        }
    }


    public static int getCurrentAppState(){
        return lifeCycleCallback.getCurrentAppState();
    }

}
